<template>
  <el-dialog title="提交申诉" :visible.sync="visible" width="1000px">
    <div class="add-form">
      <el-form ref="form" :model="form" label-width="140px">
        <el-form-item :rules="[{ required: true, message: '部门失误类型', trigger: 'change' }]" label="部门失误类型" prop="responsibleDepartmentError">
          <DepartmentTypeCascader class="w-300" filterable clearable v-model="form.responsibleDepartmentError" placeholder="部门失误类型" @afterLoadOptions="handleLoadDeOptions" ref="DepartmentTypeCascader">
          </DepartmentTypeCascader>
        </el-form-item>
        <el-form-item label="申诉原因说明" prop="complaintContent" :rules="[{ required: true, message: '申诉原因说明', trigger: 'blur' }]">
          <el-input type="textarea" v-model="form.complaintContent" placeholder="申诉原因说明" maxlength="300" show-word-limit rows="5"></el-input>
        </el-form-item>
        <el-form-item label="证明材料图片上传">
          <XmUpload
            :file-list="fileList"
            :on-remove="handleImgRemove"
            :on-success="handleUploadSuccess"
            :before-upload="handleBeforeUpload"
          ></XmUpload>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { submitComplaints } from '@/api/workorder'
export default {
  data() {
    return {
      visible: false,
      detailInfo: {},
      form: {
        responsibleDepartmentError: [],
        complaintContent: '',
      },
      fileList: [],
    }
  },
  methods: {
    show({ data }) {
      this.visible = true
      this.detailInfo = data || {}
      this.clearForm()
    },
    clearForm() {
      this.form = {
        responsibleDepartmentError: [],
        complaintContent: '',
      }
      this.fileList = []
    },
    handleUploadSuccess(response, file, fileList) {
      this.fileList = fileList
    },
    handleImgRemove(file, fileList) {
      this.fileList = fileList
    },
    handleBeforeUpload(file) {
      const sizeLimit = file.size / 1024 / 1024 < 20;
      if (file.type !== "image/jpeg" && file.type !== "image/png") {
        this.$message.error("图片只能是 JPG 格式 与 png格式!");
        return false;
      }
      if (!sizeLimit) {
        this.$message.error("图片大小不能超过 20MB!");
        return false;
      }
    },
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          submitComplaints(this.getParams()).then(res => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: `提交成功！`
              })
              this.visible = false
              this.$emit('submit')
            } else {
              this.$message({
                type: 'error',
                message: res.data.msg
              })
            }
          })
        }
      })
    },
    getParams() {
      const extJson = {
        appealImgs: this.fileList.map(item => item.response.data)
      }
      return {
        orderId: this.detailInfo.orderId,
        complaintContent: this.form.complaintContent,
        responsibleDepartmentError: this.form.responsibleDepartmentError && this.form.responsibleDepartmentError.join(','),
        extJson: JSON.stringify(extJson),
      }
    },
    handleLoadDeOptions({options}) {
      const product = options.find(item => {
        return item.label && item.label.includes('生产中心')
      })
      const filterOptions = product.children || []
      this.$refs.DepartmentTypeCascader.setOptions(filterOptions)
    }
  },
}
</script>

<style lang="scss" scoped>
</style>
